/*----------------------------------------------------------
* FILE: StrutsQueryAbstractAction.java
* LOGICAL NAME: StrutsQueryAbstractAction
* PRODUCT: jsisgsi-web
*----------------------------------------------------------
*/

package bila.framework.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import bila.framework.commons.Constants;

/**
 * @author CEL
 * <b>Created by </b>WJuarez
 * <b>for </b>jsisgsi-web
 * <b>on </b>23/07/2009 13:49:22
 * <b>Purpose</b> 
 * <p>
 * 		Action General para Consultas
 * </p>
 */
public abstract class StrutsQueryAbstractAction<Dto> extends DispatchAction {
	
	private Log log = LogFactory.getLog(StrutsQueryAbstractAction.class);
	private String listMsg;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private ActionMapping mapping;
	private ActionForm form;
	private boolean controlList = false;
	
	public abstract Dto convertForm(Dto dto) throws Exception;
		
	public abstract List<Dto> getByExampleServices(Dto dto) throws Exception;
	
	public abstract void initService(Dto dto) throws Exception;
	
	public abstract void displayFormService(Dto dto) throws Exception;
	
	public String getListMsg() {
		return listMsg;
	}

	public void setListMsg(String listMsg) {
		this.listMsg = listMsg;
	}

	/**
	 * Metodo inicial
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Dto dto = (Dto) form;
		try{
			setRequest(request);
			setResponse(response);
			setMapping(mapping);
			setForm(form);
			initService(dto);
		}catch(Exception e){
			e.printStackTrace();
			log.error(e);
			request.setAttribute(Constants.ACTION_MSG, Constants.INIT_ERROR);
		}
		return mapping.findForward(Constants.SUCCESS);
	}
	
	/**
	 * Action para recuperar registros
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getByExample(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception{
		try{
			setRequest(request);
			setResponse(response);
			setMapping(mapping);
			setForm(form);
			List<Dto> list = getByExampleServices( (Dto) form);
			request.setAttribute(listMsg, list);	
		}catch(Exception e){
			e.printStackTrace();
			request.setAttribute(Constants.ACTION_MSG, Constants.FIND_ERROR);
			log.error(e);
		}
		if(!controlList)
			return mapping.findForward(Constants.SUCCESS);
		else
			return mapping.findForward(Constants.LIST);	
	}

	/**
	 * Action para displaytag de listas
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getList(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception{
		try{
			setRequest(request);
			setResponse(response);
			setMapping(mapping);
			setForm(form);
			List<Dto> list = getByExampleServices( (Dto) form);
			request.setAttribute(listMsg, list);	
		}catch(Exception e){
			e.printStackTrace();
			request.setAttribute(Constants.ACTION_MSG, Constants.FIND_ERROR);
			log.error(e);
		}
		return mapping.findForward(Constants.LIST);
	}
	
	@SuppressWarnings({ "unchecked" })
	public ActionForward displayForm(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception{
		try{
			setRequest(request);
			setResponse(response);
			setMapping(mapping);
			setForm(form);
			displayFormService( (Dto) form);
		}catch(Exception e){
			request.setAttribute(Constants.ACTION_MSG, Constants.FIND_ERROR);
		}
		return mapping.findForward(Constants.DISPLAYFORM);
	}

	
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public ActionMapping getMapping() {
		return mapping;
	}

	public void setMapping(ActionMapping mapping) {
		this.mapping = mapping;
	}

	public ActionForm getForm() {
		return form;
	}

	public void setForm(ActionForm form) {
		this.form = form;
	}

	public Log getLog() {
		return log;
	}
	
	public boolean isControlList() {
		return controlList;
	}

	public void setControlList(boolean controlList) {
		this.controlList = controlList;
	}
	
}//class
